<?php
/**
 * Created by PhpStorm.
 * User: pf
 * Date: 2017/12/15
 * Time: 14:10
 */

namespace App\Repositories;


use App\Scenic;
use App\ScenicBuy;
use App\Setting;
use Illuminate\Support\Facades\Auth;

class ScenicRepository
{
    public function getList()
    {

    }
    public function getDetail($scenic_id){
        return Scenic::where('status',1)->where('id',$scenic_id)->firstOrFail();
    }
    public function checkScenicAuth($scenic_id){
        //是否是试听的音乐
        if($scenic_id==Setting::getConfig('sceneId')){
            return true;
        }
        //是否首次试听
        $user=Auth::guard('member')->user();
        if($user->is_listen==0){
            $user->is_listen=1;
            $scenicBuy=new ScenicBuy();
            $scenicBuy->member_id=$user->id;
            $scenicBuy->scenic=$scenic_id;
            $scenicBuy->save();
            $user->save();
            return true;
        }
        //检查以前是否观看过
        $isBuy=ScenicBuy::where('scenic',$scenic_id)
            ->where('member_id',$user->id)
            ->first();
        if(!$isBuy){
            if($user->wallet_spots>0){
                $user->decrement('wallet_spots',1);
                $scenicBuy=new ScenicBuy();
                $scenicBuy->member_id=$user->id;
                $scenicBuy->scenic=$scenic_id;
                $scenicBuy->save();
                return true;
            }
            return false;
        }
        return true;
    }
}